package com.tenement.utils.emailcode;

import java.util.Random;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

/**
 * 邮箱验证码工具类 用于注册帐号和找回密码
 * 
 * @author wk
 *
 */
public class EmailSecurityCodeUtil {

	/**
	 * 获取邮箱验证码
	 * 
	 * @param emailaddress
	 * @return
	 */
	public static String getEmailSecurityCode(String emailaddress) {
		
		String code = productCode();
		try {
			HtmlEmail email = new HtmlEmail();
			email.setHostName("smtp.126.com");// smtp服务
			email.setCharset("UTF-8"); // 设置编码格式
			email.addTo(emailaddress); // 收件地址
			email.setFrom("k3113k@126.com","北海银滩阳光假日");
			email.setAuthentication("k3113k@126.com", "k3113k");//此处填写发送方的邮箱地址和客户端授权码
			email.setSubject("北海银滩阳光假日酒店");
			email.setMsg("您的验证码为：【<h1>"+code+"</h1>】");//发送的内容
			email.send();//发送
		} catch (EmailException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return code;
		
	}

	/**
	 * 生成六位数的验证码
	 * 
	 * @return
	 */
	public static String productCode() {

		String code = "";

		Random random = new Random();

		for (int i = 0; i < 6; i++) {

			code += random.nextInt(10);

		}
		return code;
	}

	public static void main(String[] args) {
		System.out.println(getEmailSecurityCode("1357760345@qq.com"));
	}
}
